package com.lxq.opengltest02.Renderer;

import android.content.Context;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
import android.opengl.Matrix;
import android.util.Log;

import com.lxq.opengltest02.R;
import com.lxq.opengltest02.Util.BufferUtil;
import com.lxq.opengltest02.Util.ShaderUtil;
import com.lxq.opengltest02.Util.TextReaderUtil;

import java.nio.FloatBuffer;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL;
import javax.microedition.khronos.opengles.GL10;

/**
 * 作者： lxq on 2017/7/10 10:12
 * 邮箱： 842202389@qq.com
 * 用途：
 */

public class FirstRenderer implements GLSurfaceView.Renderer {

    private static int Position_Cout = 4;
    private static int Color_Cout = 3;
    private static int Bytes_count = 4;
    private static int Stride = (Position_Cout + Color_Cout) * Bytes_count;  //取值的间隔


    float[] tableVer = {
            0f, 0f, 0f, 1.5f, 1f, 1f, 1f,
            -0.5f, -0.8f, 0f, 1f, 0.7f, 0.7f, 0.7f,
            0.5f, -0.8f, 0f, 1f, 0.7f, 0.7f, 0.7f,
            0.5f, 0.8f, 0f, 2f, 0.7f, 0.7f, 0.7f,
            -0.5f, 0.8f, 0f, 2f, 0.7f, 0.7f, 0.7f,
            -0.5f, -0.8f, 0f, 1f, 0.7f, 0.7f, 0.7f,

            -0.5f, 0f, 0f, 1.5f, 1f, 0f, 0f,
            0.5f, 0f, 0f, 1.5f, 1f, 0f, 0f,

            0f, -0.4f, 0f, 1.25f, 0f, 0f, 1f,
            0f, 0.4f, 0f, 1.75f, 1f, 0f, 0f
    };
    private FloatBuffer tabelBuffer;

    private Context context;
    private int programId;

    //private static final String U_COLOR = "u_Color";
    private static final String A_COLOR = "a_Color";
    private int aColorLocation;

    private static final String A_POSITION = "a_Position";
    private int aPositionLocation;


    //做正交投影和透视投影的矩阵变换
    private static final String U_MATRIX = "u_Matrix";
    private final float[] projectionMatrix = new float[16];
    private int uMatrixLocation;


    public FirstRenderer(Context context) {
        this.context = context;
    }

    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        GLES20.glClearColor(1.0f, 1.0f, 1.0f, 0.0f);//清空屏幕颜色

        programId = ShaderUtil.getProgramId(context);

        GLES20.glUseProgram(programId);

        aColorLocation = GLES20.glGetAttribLocation(programId, A_COLOR);
        aPositionLocation = GLES20.glGetAttribLocation(programId, A_POSITION);

        tabelBuffer = BufferUtil.floatTOBuffer(tableVer);

        GLES20.glVertexAttribPointer(aPositionLocation, Position_Cout, GLES20.GL_FLOAT, false, Stride, tabelBuffer);
        GLES20.glEnableVertexAttribArray(aPositionLocation);

        GLES20.glVertexAttribPointer(aColorLocation, Color_Cout, GLES20.GL_FLOAT, false, Stride, tabelBuffer);
        GLES20.glEnableVertexAttribArray(aColorLocation);

        uMatrixLocation = GLES20.glGetUniformLocation(programId, U_MATRIX);
    }

    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {
        GLES20.glViewport(0, 0, width, height);

        float ratio = width > height ? (float) width / (float) height : (float) height / (float) width;
        //做正交投影  变换
        if (width > height) {
            Matrix.orthoM(projectionMatrix, 0, -ratio, ratio, -1f, 1f, -1f, 1f);
        } else {
            Matrix.orthoM(projectionMatrix, 0, -1f, 1f, -ratio, ratio, -1f, 1f);
        }
    }

    @Override
    public void onDrawFrame(GL10 gl) {
        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);//清空屏幕，擦除屏幕所有颜色，并用管理ClearColor颜色填充整个屏幕

        GLES20.glUniformMatrix4fv(uMatrixLocation, 1, false, projectionMatrix, 0);


        //GLES20.glUniform4f(uColorLocation, 0.3f, 0.3f, 0.3f, 1.0f);
        GLES20.glDrawArrays(GLES20.GL_TRIANGLE_FAN, 0, 6);

        //GLES20.glUniform4f(uColorLocation, 1.0f, 0.0f, 0.0f, 1.0f);
        GLES20.glDrawArrays(GLES20.GL_LINES, 6, 2);

        //GLES20.glUniform4f(uColorLocation, 1.0f, 0.0f, 0.0f, 1.0f);
        GLES20.glDrawArrays(GLES20.GL_POINTS, 8, 1);

        //GLES20.glUniform4f(uColorLocation, 1.0f, 0.0f, 0.0f, 1.0f);
        GLES20.glDrawArrays(GLES20.GL_POINTS, 9, 1);
    }
}
